package HGR;


import com.jogamp.opengl.util.Animator;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.awt.GLJPanel;
import javax.media.opengl.glu.GLU;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;


public class OGL implements GLEventListener
{
    public static GL2 gl;
    public static GLU glu;
   public static int width;
   public static int heigth;
   public float sc = 2.0f;
   public static float rotx=0.0f;
   public static float roty=0.0f;
   public static float rotz=0.0f;
   public static float trax=0.0f;
   public static float tray=0.0f;
   public static float traz=0.0f;
   public static float escx=1.0f;
   public static float escy=1.0f;
   public static float escz=1.0f;
   
   public static float zoom=-15.0f;

   public OGL(int w, int h)
   {
       width = w;
       heigth = h;
   }

   


   public static void main(String[] args)
   {
       //System.out.println(System.getProperty("java.library.path"));
       //System.setProperty("java.library.path", "./lib");
       //System.out.println(System.getProperty("java.library.path"));
       OGL Graphic = new OGL(500, 340);

       System.out.println("Initcio");
       JFrame frame = new JFrame("JOGL");
       JPanel pan = new JPanel();
       JLayeredPane layers = new JLayeredPane();
       
       GLJPanel canvas = new GLJPanel();
       canvas.setPreferredSize(new Dimension(Graphic.width, Graphic.heigth));
       canvas.setBounds(0, 0, Graphic.width, Graphic.heigth);
       canvas.addGLEventListener(Graphic);
       
       pan.setBounds(0, 0, Graphic.width, Graphic.heigth);
       pan.add(canvas);

       layers.add(pan, new Integer(1));

       final Animator animator = new Animator(canvas);
       frame.addWindowListener(new WindowAdapter() {

           @Override
           public void windowClosing(WindowEvent e) {

               new Thread(new Runnable() {

                   public void run() {
                       animator.stop();
                       System.exit(0);
                   }
               }).start();
           }
       });


       frame.getContentPane().add(layers, BorderLayout.CENTER);       
       frame.setSize(Graphic.width, Graphic.heigth);
       frame.setLocationRelativeTo(null);
       frame.setVisible(true);
       animator.start();
       System.out.println("Fin");
       
   }

   public void init(GLAutoDrawable drawable)
   {
       //HGR_gui.animator.pause();
       gl = drawable.getGL().getGL2();
       gl.glEnable(gl.GL_BLEND);
       gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA);
       gl.glShadeModel(gl.GL_FLAT);
       gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
       gl.glEnable(gl.GL_DEPTH_TEST);
       gl.setSwapInterval(1);
       reshape(drawable, 0, 0, width, heigth);
   }


   public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
   {
       GL2 _gl = drawable.getGL().getGL2();
       glu = new GLU();
       glu.createGLU();
       gl.glClearColor(0.48f,0.55f,0.63f,0);
       //_gl.glColor3f(0.0f, 0.0f, 0.0f);
       _gl.glViewport(0,0,width,height);
       _gl.glMatrixMode(gl.GL_PROJECTION);
       _gl.glLoadIdentity();
       glu.gluPerspective(45.0,(float)(4.0/3.0), 1.0, 100.0);
       _gl.glMatrixMode(gl.GL_MODELVIEW);
       //_gl.glLoadIdentity();
   }

   public void display(GLAutoDrawable drawable) {
       System.out.println("OGL");
       gl = drawable.getGL().getGL2();
       gl.glLoadIdentity();
       gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
       //gl.glClearColor(170f/255f, 170f/255f, 170f/255f, 0);
       gl.glClearColor(0.48f,0.55f,0.63f,0);
       
       gl.glTranslatef(0.0f,0.0f,zoom);
       gl.glRotatef(30.0f,1.0f,0.0f,0.0f);
       gl.glRotatef(-45.0f,0.0f,1.0f,0.0f);
       gl.glRotatef(0.0f,0.0f,0.0f,1.0f);

       ejes();
       gl.glTranslatef(-trax,-tray,-traz);
       gl.glRotatef(rotx,1.0f,0.0f,0.0f);
       gl.glRotatef(roty,0.0f,1.0f,0.0f);
       gl.glRotatef(rotz,0.0f,0.0f,1.0f);
       gl.glScalef(escx, escy, escz);
       
       cubo();
       borde();
       
       gl.glFlush();
   }

   public void borde(){
       gl.glPushMatrix();
           gl.glBegin(gl.GL_LINES);
               gl.glColor3f(0.4f, 0.4f, 0.4f);
               gl.glVertex3f(0.0f,0,0.0f);
               gl.glVertex3f(0.0f,0.0f,sc);

               gl.glVertex3f(0.0f,0.0f,sc);
               gl.glVertex3f(0.0f,sc,sc);

               gl.glVertex3f(0.0f,sc,sc);
               gl.glVertex3f(0.0f,sc,0.0f);

               gl.glVertex3f(0.0f,sc,0.0f);
               gl.glVertex3f(0.0f,0,0.0f);

               //
               gl.glVertex3f(sc,0,0.0f);
               gl.glVertex3f(sc,0.0f,sc);

               gl.glVertex3f(sc,0.0f,sc);
               gl.glVertex3f(sc,sc,sc);

               gl.glVertex3f(sc,sc,sc);
               gl.glVertex3f(sc,sc,0.0f);

               gl.glVertex3f(sc,sc,0.0f);
               gl.glVertex3f(sc,0,0.0f);

               //
               gl.glVertex3f(0.0f,0.0f,0.0f);
               gl.glVertex3f(sc,0.0f,0.0f);

               gl.glVertex3f(0.0f,sc,0.0f);
               gl.glVertex3f(sc,sc,0.0f);

               gl.glVertex3f(0.0f,sc,sc);
               gl.glVertex3f(sc,sc,sc);

               gl.glVertex3f(0.0f,0.0f,sc);
               gl.glVertex3f(sc,0.0f,sc);

           gl.glEnd();
       gl.glPopMatrix();
   }

   public void cubo(){
       gl.glPushMatrix();
       gl.glBegin(gl.GL_POLYGON);
           gl.glColor3f(0.8f, 0.8f, 0.8f);
               gl.glVertex3f(0.0f,0.0f,0.0f);
               gl.glVertex3f(0.0f,0.0f,sc);

               gl.glVertex3f(0.0f,sc,sc);
               gl.glVertex3f(0.0f,sc,0.0f);
           gl.glEnd();
           gl.glBegin(gl.GL_POLYGON);
               gl.glColor3f(0.7f, 0.7f, 0.7f);

               gl.glVertex3f(0.0f,0.0f,0.0f);
               gl.glVertex3f(0.0f,sc,0.0f);

               gl.glVertex3f(sc,sc,0.0f);
               gl.glVertex3f(sc,0.0f,0.0f);
           gl.glEnd();

           gl.glBegin(gl.GL_POLYGON);
               gl.glColor3f(0.9f, 0.9f, 0.9f);

               gl.glVertex3f(sc,0.0f,0.0f);
               gl.glVertex3f(sc,sc,0.0f);

               gl.glVertex3f(sc,sc,sc);
               gl.glVertex3f(sc,0.0f,sc);
           gl.glEnd();

           gl.glBegin(gl.GL_POLYGON);
               gl.glColor3f(0.75f, 0.75f, 0.75f);

               gl.glVertex3f(0.0f,0.0f,sc);
               gl.glVertex3f(0.0f,sc,sc);

               gl.glVertex3f(sc,sc,sc);
               gl.glVertex3f(sc,0.0f,sc);
           gl.glEnd();


           gl.glBegin(gl.GL_POLYGON);
               gl.glColor3f(0.85f, 0.85f, 0.85f);

               gl.glVertex3f(0.0f,sc,0.0f);
               gl.glVertex3f(sc,sc,0.0f);

               gl.glVertex3f(sc,sc,sc);
               gl.glVertex3f(0.0f,sc,sc);
           gl.glEnd();
           gl.glBegin(gl.GL_POLYGON);
               gl.glColor3f(0.78f, 0.78f, 0.78f);

               gl.glVertex3f(0.0f,0.0f,0.0f);
               gl.glVertex3f(sc,0.0f,0.0f);

               gl.glVertex3f(sc,0.0f,sc);
               gl.glVertex3f(0.0f,0.0f,sc);
           gl.glEnd();
         gl.glPopMatrix();
   }
   public void ejes(){
       gl.glPushMatrix();
           gl.glBegin(gl.GL_LINES);
               gl.glColor3f(0.0f, 0.0f, 0.5f);
               gl.glVertex3f(0.0f,0,0.0f);
               gl.glVertex3f(0.0f,0,1000.0f);

               gl.glColor3f(0.0f, 0.5f, 0.0f);
               gl.glVertex3f(0.0f,0,0.0f);
               gl.glVertex3f(0.0f,1000,0.0f);

               gl.glColor3f(0.5f, 0.0f, 0.0f);
               gl.glVertex3f(0.0f,0.0f,0.0f);
               gl.glVertex3f(1000.0f,0.0f,0.0f);
           gl.glEnd();
       gl.glPopMatrix();
   }

   public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
   }

   public void dispose(GLAutoDrawable glad)
   {
       System.out.println("bye");
   }


}